package com.clp.protocol.iec104.client.event;

import com.clp.protocol.core.client.ClientEvent;
import com.clp.protocol.core.utils.AssertUtil;
import com.clp.protocol.iec104.client.Iec104Client;
import lombok.Getter;

/**
 * 客户端连接事件
 */
@Getter
public class ClientConnectEvent extends ClientEvent<Iec104Client> {
    private final Type type;
    private final String remoteHost;
    private final int remotePort;

    public ClientConnectEvent(Iec104Client source, Throwable cause, Type type, String remoteHost, int remotePort) {
        super(source, cause);
        AssertUtil.nonNull(type);
        AssertUtil.nonNull(remoteHost);
        this.type = type;
        this.remoteHost = remoteHost;
        this.remotePort = remotePort;
    }

    public ClientConnectEvent(Iec104Client source, Type type, String remoteHost, int remotePort) {
        this(source, null, type, remoteHost, remotePort);
    }

    public enum Type {
        /**
         * 开始新连接前
         */
        NEW_CONNECT_BEFORE,

        /**
         * 新连接 成功
         */
        NEW_CONNECT_SUCCESS,

        /**
         * 新连接失败
         */
        NEW_CONNECT_FAILED,

        /**
         * 重连前
         */
        RE_CONNECT_BEFORE,

        /**
         * 重连成功
         */
        RE_CONNECT_SUCCESS,

        /**
         * 重连失败
         */
        RE_CONNECT_FAILED,

        /**
         * 断开连接前
         */
        DIS_CONNECT_BEFORE,

        /**
         * 断开连接成功
         */
        DIS_CONNECT_SUCCESS,

        /**
         * 断开连接失败
         */
        DIS_CONNECT_FAILED;

        /**
         * 能改变容器的size()
         * @return
         */
        public boolean canChangeContainerSize() {
            return this == NEW_CONNECT_SUCCESS
                    || this == RE_CONNECT_SUCCESS
                    || this == DIS_CONNECT_SUCCESS;
        }
    }
}
